/**
 * 导出学生实习列表
 * 
 * /xingzheng/student/export
 */

const Student = require('../../../models/internshipStudents')
const User_Info = require('../../../models/usersInfo')
const utils = require('../../../utils/index')
const $ = require('../../../utils/common')

const export_student = function(req,res) {
    try{
        if (!req.body.year_id) {
            $.sendMessage(res, 1015, 'year_id 不存在')
            return
        }

        Student.belongsTo(User_Info,
            {
                foreignKey: 'student_user_name', 
                targetKey: 'user_name'
            })

        Student.findAndCountAll({
            where: {
                student_year: req.body.year_id
            },
            // 排序
            order: [
                ['createdAt', 'DESC'],
            ],
            // 返回的属性
            include: [
                {
                    model: User_Info,
                    attributes: ['user_fullname' ,'user_major', 'user_class'], 
                }
            ],
            raw: true,
            attributes: ['student_user_name' ,'company_name', 'teacher_user_fullname', 
            'student_status' ,'createdAt']

        }).then(result => {
            res.json({
                code: 200,
                message: 'success',
                data: result.rows,
            })
            // 生成一条操作日志
            utils.create_a_log(req, '实习学生数据', '导出')
        })
        return
    }

    catch(error) {
        $.sendMessage(res, 0000, 'error' + error)
    }
}

module.exports = export_student